package com.gatesim;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.Log;

public class AndGate extends Gate{
	
	private static int initResource = 0;
	//dist from top corner
	private static int ANDGATE_TOPPIN;
	private static int ANDGATE_BOTPIN;
	private static int ANDGATE_FNTPIN;
	
    private Bitmap picToDraw;
    
    private Paint paint;
	private static int and_width;
	private static int and_height;
	
	   protected static Bitmap defaultPic;
	   protected static Bitmap selectedPic;
	   protected static Bitmap chargedPic;
	
	protected AndGate(Point c) {
		super(c);
		
		if(initResource == 0){
			initAnd();
			initResource++;
		}	

		corner.set(center.x - (and_width/2), center.y - (and_height/2) );
		calcPins();
		paint = new Paint();
		
	    picToDraw = defaultPic;
        width  = and_width;
        height = and_height;
	}
	
	public static void initAnd(){
	    BitmapFactory.Options options = new BitmapFactory.Options();
	    options.inTempStorage = new byte[16*1024];
	    
		//Load image, 1x
	    // Determine whether to use cellphone version of non cellphone
	    if ( false ){
    		defaultPic  =        BitmapFactory.decodeResource(resources, R.drawable.and, options);
    		selectedPic =        BitmapFactory.decodeResource(resources, R.drawable.selectedand, options);
    		chargedPic  =        BitmapFactory.decodeResource(resources, R.drawable.and_charged, options);
	    }else{
	        defaultPic =         BitmapFactory.decodeResource(resources, R.drawable.and_cp, options);
            selectedPic = BitmapFactory.decodeResource(resources, R.drawable.selectedand_cp, options);
            chargedPic=  BitmapFactory.decodeResource(resources, R.drawable.and_charged_cp, options);
	    }
    
		//Set Constants
		ANDGATE_TOPPIN = defaultPic.getHeight()/5; 
		ANDGATE_BOTPIN = defaultPic.getHeight()*4/5;
		ANDGATE_FNTPIN = defaultPic.getHeight()/2;
		//Will be the same for each object bc picture is the same.
		and_width  =  defaultPic.getWidth();
		and_height = defaultPic.getHeight();
	}
	
	private void calcPins(){
		topPin = new InputConnection(new Point(corner.x, corner.y + ANDGATE_TOPPIN));
		botPin = new InputConnection(new Point(corner.x,corner.y + ANDGATE_BOTPIN));
		fntPin = new OutputConnection(new Point(corner.x + and_width,corner.y + ANDGATE_FNTPIN));
	}
	
	public void recalcPins(){
		topPin.setPoint(corner.x, corner.y + ANDGATE_TOPPIN);
		botPin.setPoint(corner.x,corner.y + ANDGATE_BOTPIN);
		fntPin.setPoint(corner.x + and_width,corner.y + ANDGATE_FNTPIN);		
	}
	

	@Override
	public int produceOut(){ 
		// And its children together
		int out = 1;
        
//		i = new Integer(i.intValue() + delayValue);
		
		for(Wire w: parents){
			if(w == null){
				fntPin.setCharge(Connection.CONN_NO_CHARGE);
				break;
			} else {
				w.produceOut(); //getGateSource().produceOut();//i);
				w.relayCharge();
				
				if ( GateWorld.animationMode && (this.personalDelay < (w.personalDelay + this.delayValue)) ){
				    personalDelay = new Integer(w.personalDelay + this.delayValue);
				}
				
				
				if( w.getDest().getCharge() == Connection.CONN_NO_CHARGE || 
		    	                        out == Connection.CONN_NO_CHARGE) {
					out = Connection.CONN_NO_CHARGE;
				} else {
					out = out & w.getDest().getCharge();
				}
				fntPin.setCharge(out);
			}
		}
		
//        personalDelay = new Integer(personalDelay.intValue() + delayValue);
//        if ( personalDelay.intValue() < i.intValue() ){
//          personalDelay = new Integer(i.intValue());
//        }

//		if(GateWorld.animationMode){
	        if ( _gCount == personalDelay.intValue() ){ //i.intValue() ){
	    		if(fntPin.getCharge() == Connection.CONN_ONE_CHARGE){
	    			picToDraw = chargedPic;
	    		}else{
	    		    picToDraw = defaultPic;
	    		}
	    		_gCount = 0;
			}
		    _gCount++;
//		}

		return fntPin.getCharge();
	}
	
	public void endSimulationMode(){
		picToDraw = defaultPic;
	    _gCount = 0;
	    personalDelay = new Integer(0);
	}

	@Override
	public void draw(Canvas c,Resources r, boolean simulationMode) {
		c.drawBitmap(picToDraw, corner.x, corner.y, null);		
		fntPin.draw(c, r);
		topPin.draw(c, r);
		botPin.draw(c, r);
		if(simulationMode) {
			paint.setTextSize(50);
			paint.setColor(Color.RED);
			c.drawText(""+((fntPin.getCharge() == Connection.CONN_NO_CHARGE) ? "X" : fntPin.getCharge()), 
					fntPin.getPoint().x + 20, fntPin.getPoint().y, paint);
		}
	}
	
	@Override
	public int getWidth(){
		return and_width;
	}
	@Override
	public int getHeight(){
		return and_height;
	}

   @Override
    public void select(boolean b, boolean simulationMode){
	   if(!simulationMode){
		   picToDraw = (b) ? selectedPic : defaultPic; 
	   }
    }
	
}
